package com.zhiche.lisa.lspm.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.lspm.dao.model.trailer.TrailerInfo;
import com.zhiche.lisa.lspm.dao.model.trailer.bo.ModifyTrailerInfoBO;
import com.zhiche.lisa.lspm.dao.model.trailer.bo.TrailerCarrierBO;
import com.zhiche.lisa.lspm.dao.model.trailer.bo.TrailerInfoLineBO;
import com.zhiche.lisa.lspm.service.trailer.TrailerInfoService;
import com.zhiche.lisa.lspm.vo.common.CommonVo;
import com.zhiche.lisa.lspm.vo.lsp.LspSubVo;
import com.zhiche.lisa.lspm.vo.vehicle.TrailerBasicVO;
import com.zhiche.lisa.lspm.vo.vehicle.TrailerDetailVO;
import com.zhiche.lisa.lspm.vo.vehicle.TrailerInfoLineVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 挂车 接口
 *
 * @author lbl
 * @since 2018-05-28
 */
@RestController
@RequestMapping("/trailer")
@Api(value = "/trailer", description = "挂车接口", tags = {"挂车接口"})
public class TrailerInfoController {

    private static final Logger LOGGER = LoggerFactory.getLogger(TrailerInfoController.class);

    @Autowired
    private TrailerInfoService trailerInfoService;

    @PostMapping(value = "/trailerInfoLine")
    @ApiOperation(value = "获取挂车基础信息列表", notes = "获取挂车基础信息列表", produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse<Page<TrailerInfoLineVO>> trailerInfoLine(@RequestBody Page<TrailerInfoLineBO> page, @RequestHeader("Authorization") String authorization) {
        LOGGER.info("TrailerInfoController.trailerInfoLine（获取挂车基础信息列表） params : {} ", page);

        RestfulResponse<Page<TrailerInfoLineVO>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);

        try {
            Page<TrailerInfoLineVO> pageVo = trailerInfoService.trailerInfoLine(page, authorization);
            result.setData(pageVo);
        } catch (BaseException e) {
            LOGGER.error("TrailerInfoController.trailerInfoLine ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("TrailerInfoController.trailerInfoLine ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }

        return result;
    }

    @PostMapping(value = "/authedtrailerLine")
    @ApiOperation(value = "获取认证的挂车信息列表", notes = "获取认证的挂车信息列表", produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse<Page<TrailerInfoLineVO>> authedtrailerLine(@RequestBody Page<TrailerInfoLineBO> page) {
        LOGGER.info("TrailerInfoController.authedtrailerLine（获取认证的挂车信息列表） params : {} ", page);

        RestfulResponse<Page<TrailerInfoLineVO>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);

        try {
            Page<TrailerInfoLineVO> pageVo = trailerInfoService.authedTrailerInfoLine(page);
            result.setData(pageVo);
        } catch (BaseException e) {
            LOGGER.error("TrailerInfoController.authedtrailerLine ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("TrailerInfoController.authedtrailerLine ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }

        return result;
    }

    @GetMapping(value = "/findModifyInfo")
    @ApiOperation(value = "查找编辑挂车信息", notes = "查找编辑挂车信息", produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse<ModifyTrailerInfoBO> findModifyInfo(Long id) {
        LOGGER.info("TrailerInfoController.findModifyInfo（查找编辑挂车信息） params : {} ", id);

        RestfulResponse<ModifyTrailerInfoBO> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);

        try {
            result.setData(trailerInfoService.findModifyInfo(id));
        } catch (BaseException e) {
            LOGGER.error("TrailerInfoController.findModifyInfo ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("TrailerInfoController.findModifyInfo ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }

        return result;
    }

    @PostMapping(value = "/getTrailerByLsp")
    @ApiOperation(value = "获取承运商下的挂车", notes = "获取承运商下的挂车", produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse<Page<TrailerDetailVO>> getTrailerByLsp(@RequestBody Page<TrailerDetailVO> page, @RequestHeader("Authorization") String authorization) {
        LOGGER.info("TrailerInfoController.getTrailerByLsp（获取承运商下的挂车） params : {} , {}", page);

        RestfulResponse<Page<TrailerDetailVO>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(trailerInfoService.getTrailerByLsp(page, authorization));
        } catch (BaseException e) {
            LOGGER.error("TrailerInfoController.getTrailerByLsp ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("TrailerInfoController.getTrailerByLsp ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }

        return result;
    }

    @PostMapping(value = "/discontinuatTrailer")
    @ApiOperation(value = "停用挂车", notes = "停用挂车", produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse discontinuatTrailer(@RequestBody CommonVo commonVo) {
        LOGGER.info("TrailerInfoController.discontinuatTrailer（停用挂车） params : {} ", commonVo.toString());
        RestfulResponse result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            List<String> msg = trailerInfoService.discontinuatTrailer(commonVo.getCarrierIds());
            if (!CollectionUtils.isEmpty(msg)) {
                result = new RestfulResponse<>(CommonEnum.ERRORLIST.getCode(), "部分停用失败", msg);
            }
        } catch (BaseException e) {
            LOGGER.error("TrailerInfoController.discontinuatTrailer ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("TrailerInfoController.discontinuatTrailer ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/enableTrailer")
    @ApiOperation(value = "启用挂车", notes = "启用挂车", produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse enableTrailer(@RequestBody CommonVo commonVo) {
        LOGGER.info("TrailerInfoController.enableTrailer（启用挂车） params : {} ", commonVo.toString());
        RestfulResponse result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            List<String> msgs = trailerInfoService.enableTrailer(commonVo.getTrailerIds());
            if (!CollectionUtils.isEmpty(msgs)) {
                result = new RestfulResponse<>(CommonEnum.ERRORLIST.getCode(), "部分启用失败", msgs);
            }
        } catch (BaseException e) {
            LOGGER.error("TrailerInfoController.enableTrailer ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("TrailerInfoController.enableTrailer ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/authTrailer")
    @ApiOperation(value = "认证挂车", notes = "认证挂车", produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse authTrailer(@RequestBody CommonVo commonVo, @RequestHeader("Authorization") String authorization) {
        LOGGER.info("TrailerInfoController.authTrailer（认证挂车） params : {} ", commonVo.toString());
        RestfulResponse result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            List<String> msg = trailerInfoService.batchUpdateAuthStatusByIds(commonVo.getTrailerIds(), authorization);
            if (!CollectionUtils.isEmpty(msg)) {
                result = new RestfulResponse<>(CommonEnum.ERRORLIST.getCode(), "部分认证失败", msg);
            }
        } catch (BaseException e) {
            LOGGER.error("TrailerInfoController.authTrailer ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("TrailerInfoController.authTrailer ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/cancelAuthTrailer")
    @ApiOperation(value = "取消认证挂车", notes = "取消认证挂车", produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse cancelAuthTrailer(@RequestBody CommonVo commonVo) {
        LOGGER.info("TrailerInfoController.cancelAuthTrailer（取消认证挂车） params : {} ", commonVo.toString());
        RestfulResponse result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            trailerInfoService.batchUpdateCancelAuthStatusByIds(commonVo.getTrailerIds());
        } catch (BaseException e) {
            LOGGER.error("TrailerInfoController.cancelAuthTrailer ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("TrailerInfoController.cancelAuthTrailer ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/deleteTrailer")
    @ApiOperation(value = "删除挂车", notes = "删除挂车", produces = MediaType.APPLICATION_JSON_VALUE, response =
            RestfulResponse.class)
    public RestfulResponse deleteTrailer(@RequestBody CommonVo commonVo) {
        LOGGER.info("TrailerInfoController.cancelAuthCarrier（删除挂车） params : {} ", commonVo.toString());
        RestfulResponse result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            List<String> msgs = trailerInfoService.deleteByIds(commonVo.getTrailerIds());
            if (!CollectionUtils.isEmpty(msgs)) {
                result = new RestfulResponse<>(CommonEnum.ERRORLIST.getCode(), "部分挂车删除失败", msgs);
            }
        } catch (BaseException e) {
            LOGGER.error("TrailerInfoController.cancelAuthCarrier ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("TrailerInfoController.cancelAuthCarrier ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    //--来源于原来driver

    /**
     * 根据司机和牵引车车牌获取牵引车信息
     */
    @GetMapping(value = "/getTrailer")
    @ApiOperation(value = "获取挂车车辆信息", notes = "获取挂车车辆信息", produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse<String> getTrailerInfoByDriverAndCarrier(Long driverId, String plate) {
        LOGGER.info("TrailerInfoController.getTrailerInfoByDriverAndCarrier param: {},{}", driverId, plate);
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), "success", null);
        try {
            String strTrailerPlate = trailerInfoService.getTrailerInfoByDriverAndCarrier(driverId, plate);
            result.setData(strTrailerPlate);
        } catch (BaseException be) {
            LOGGER.error("TrailerInfoController.getTrailerInfoByDriverAndCarrier error:{}", be.getMessage());
            result.setMessage(be.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    @GetMapping(value = "/verifyTrailer")
    @ApiOperation(value = "验证挂车信息", notes = "验证挂车信息", produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse<Boolean> verifyTrailer(Long driverId, String carrierPlate, String trailerPlate) {
        LOGGER.info("VehicleController.verifyTrailer param: {},{}，{}", driverId, carrierPlate, trailerPlate);
        RestfulResponse<Boolean> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), "success", null);
        try {
            Boolean blnRtn = trailerInfoService.verifyTrailerInfoByCarrier(driverId, carrierPlate, trailerPlate);
            result.setData(blnRtn);
        } catch (BaseException be) {
            LOGGER.error("VehicleController.verifyTrailer error:{}", be.getMessage());
            result.setMessage(be.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("VehicleController.verifyTrailer error:{}", e.getMessage());
            result.setMessage("验证挂车车辆失败,系统异常");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    @PostMapping(value = "/updateTrailerBasic")
    @ApiOperation(value = "创建挂车", notes = "创建挂车", produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse<TrailerInfo> updateTrailer(@RequestBody TrailerInfo trailerInfoParam) {
        LOGGER.info("TrailerInfoController.updateTrailer（创建挂车） params : {} ", trailerInfoParam.toString());
        RestfulResponse<TrailerInfo> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(),
                null);
        try {
            TrailerInfo lspInfo = trailerInfoService.updateTrailer(trailerInfoParam);
            result.setData(lspInfo);
        } catch (BaseException e) {
            LOGGER.error("TrailerInfoController.updateTrailer ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("TrailerInfoController.updateTrailer ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @GetMapping(value = "/getTrailerBasic")
    @ApiOperation(value = "获取牵引车", notes = "获取牵引车", produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse<TrailerBasicVO> getLsp(Long trailerId) {
        LOGGER.info("TrailerInfoController.getLsp（获取牵引车） params : {} ", trailerId);
        RestfulResponse<TrailerBasicVO> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(),
                null);
        try {
            result.setData(trailerInfoService.getTrailerInfoLine(trailerId));
        } catch (BaseException e) {
            LOGGER.error("TrailerInfoController.getLsp ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("TrailerInfoController.getLsp ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/wxCarrierTrailer")
    @ApiOperation(value = "微信端获取牵引车与挂车信息", notes = "微信端获取牵引车与挂车信息", produces = MediaType.APPLICATION_JSON_VALUE, response =
            RestfulResponse.class)
    public RestfulResponse<Page<TrailerCarrierBO>> wxCarrierTrailer(@RequestBody Page<TrailerCarrierBO> page) {
        LOGGER.info("CarrierController.wxCarrierTrailer（获取牵引车与挂车列表） params : {} ", page);

        RestfulResponse<Page<TrailerCarrierBO>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);

        try {
            Page<TrailerCarrierBO> pageVo = trailerInfoService.carrierTrailerList(page);
            result.setData(pageVo);
        } catch (BaseException e) {
            LOGGER.error("CarrierController.wxCarrierTrailer ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("CarrierController.wxCarrierTrailer ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/transferTrailer")
    @ApiOperation(value = "转移挂车", notes = "转移挂车",
            produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse<Object> transferTrailer(@RequestBody LspSubVo lspSubVo, @RequestHeader("Authorization") String authorization) {
        LOGGER.info("TrailerInfoController.transferTrailer params:{}", lspSubVo);
        RestfulResponse<Object> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), "success", null);
        try {
            result.setData(trailerInfoService.changeLsp(lspSubVo.getIds(), lspSubVo.getLspId(), lspSubVo.getIsAllTransfer(), authorization));
        } catch (BaseException be) {
            LOGGER.error("TrailerInfoController.transferTrailer error:{}", be.getMessage());
            result.setMessage(be.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("TrailerInfoController.transferTrailer error:{}", e.getMessage());
            result.setMessage("操作失败");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    @PostMapping(value = "/updateDeleteStatus")
    @ApiOperation(value = "恢复挂车删除状态", notes = "恢复挂车删除状态", produces = MediaType.APPLICATION_JSON_VALUE, response
            = RestfulResponse.class)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "操作失败", response = Object.class)})
    public RestfulResponse<List<String>> updateDeleteStatus(@RequestBody List<Long> ids) {
        LOGGER.info("CarrierController.updateDeleteStatus（恢复挂车删除状态） params : {} ", ids);
        RestfulResponse<List<String>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum
                .SUCCESS.getText(), null);
        try {
            List<String> msgs = trailerInfoService.updateDeleteStatus(ids);
            if (!CollectionUtils.isEmpty(msgs)) {
                result = new RestfulResponse<>(CommonEnum.ERRORLIST.getCode(), "部分挂车删除失败", msgs);
            }
        } catch (BaseException e) {
            LOGGER.error("CarrierController.updateDeleteStatus ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("CarrierController.updateDeleteStatus ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        }
        return result;
    }
}
